package radio.vnmedia.vn;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class NotifierHelper {
	private static final int NOTIFICATION_ID = 0x1001;
	public static boolean sentNotificationPlaylist = false;

	public static void sendNotification(Activity caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean flashLed, boolean vibrate) {

		NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);

		// Create this outside the button so we can increment the number drawn
		// over the notification icon.
		// This indicates the number of alerts for this event.
		final Notification notify = new Notification(R.drawable.icon, title, System.currentTimeMillis());

		if (numberOfEvents > 0)
			notify.number = numberOfEvents;
		notify.flags |= Notification.FLAG_NO_CLEAR;
		if (flashLed) {
			// add lights
			notify.flags |= Notification.FLAG_SHOW_LIGHTS;
			notify.ledARGB = Color.CYAN;
			notify.ledOnMS = 500;
			notify.ledOffMS = 500;
		}

		if (vibrate) {
			// add vibs
			notify.vibrate = new long[] { 100, 200, 200, 200, 200, 200, 1000, 200, 200, 200, 1000, 200 };
		}

		Intent toLaunch = new Intent(caller, activityToLaunch);
		toLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// Get source from:
		// http://stackoverflow.com/questions/6112733/how-to-detect-sender-of-notification-sent-by-notificationmanager-detect-which
		// Bundle bundle = new Bundle();
		// bundle.putString("test", "me");
		// toLaunch.putExtras(bundle);

		PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);
		notify.setLatestEventInfo(caller, title, msg, intentBack);
		notifier.notify(NOTIFICATION_ID, notify);
	}

	public static void sendNotification2(Activity caller, Class<?> activityToLaunch, String title, int numberOfEvents) {
		String msg = numberOfEvents + "Touch to play " + numberOfEvents + " radio track in playlist";

		NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);

		final Notification notify = new Notification(R.drawable.icon, title, System.currentTimeMillis());

		if (numberOfEvents > 0)
			notify.number = numberOfEvents;
		notify.flags |= Notification.FLAG_NO_CLEAR;
		notify.flags |= Notification.FLAG_AUTO_CANCEL;
		Intent toLaunch = new Intent(caller, activityToLaunch);
		// toLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// Get source from:
		// http://stackoverflow.com/questions/6112733/how-to-detect-sender-of-notification-sent-by-notificationmanager-detect-which

		PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);
		notify.setLatestEventInfo(caller, title, msg, intentBack);
		notifier.notify(NOTIFICATION_ID + 1, notify);
		sentNotificationPlaylist = true;
	}

	public static void clear(Activity caller) {
		NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
		notifier.cancelAll();
	}
}
